package nige.capture;

import java.awt.Color;
import java.util.Vector;

public class Maillage {

	public static Vector points = new Vector();
	public static Vector triangles = new Vector();
	public static Triangles bigTriag = null;
	
	public static boolean next = false;
	
	public static void cleanPoints() {
		for(int i=0;i<points.size();i++) {
			Points pts = (Points)points.get(i);
			int indDel = points.indexOf(pts,i+1);
			if(indDel != -1) {
				System.out.println(indDel);
			}
		}
		/*
		points = new Vector();
		points.add(new Points(53,53,0,Color.blue));
		points.add(new Points(100,100,0,Color.red));
		points.add(new Points(150,60,0,Color.green));
		points.add(new Points(150,150,0,Color.blue));
		points.add(new Points(200,60,0,Color.green));
		points.add(new Points(150,200,0,Color.blue));
		 */
	}
	
	public static void createMaillage() {
		cleanPoints();
		createBigTriangle();
		constructTriags();
		transDelaunay();
		eraseBigTriag();
		Fenetre.imageTransformee.repaint();
	}
	
	public static void createBigTriangle() {
		int xMin = 1000000;
		int xMax = -1000000;
		int yMin = 1000000;
		int yMax = -1000000;
		for(int i=0;i<points.size();i++) {
			Points pts = (Points)points.get(i);
			if(pts.xValue > xMax) {
				xMax = pts.xValue;
			}
			if(pts.xValue < xMin) {
				xMin = pts.xValue;
			}
			if(pts.yValue > yMax) {
				yMax = pts.yValue;
			}
			if(pts.yValue < yMin) {
				yMin = pts.yValue;
			}
		}
		int longueur = xMax - xMin;
		int hauteur = yMax - yMin;
		bigTriag = new Triangles(
				new Points((xMax+xMin)/2,yMin-hauteur,0,Color.black),
				new Points(xMin-2*longueur,yMax+hauteur/2,0,Color.black),
				new Points(xMax+2*longueur,yMax+hauteur/2,0,Color.black)
				);
		triangles.add(bigTriag);
	}
	
	public static void constructTriags() {
		boolean isNotIntriag = false;
		for(int i=0;i<points.size();i++) {
			isNotIntriag = true;
			Points pts = (Points)points.get(i);
			for(int j=0;j < triangles.size();j++) {
				Triangles triag = (Triangles)triangles.get(j);
				if(triag.isInTriag(pts)) {
					createNewTriag(pts,triag,j);
					j = triangles.size();
					isNotIntriag = false;
				}
			}
			if(isNotIntriag) {
				points.remove(i);
			}
		}
	}
	
	/**
	 * y = ax + b
	 */
	public static boolean isInTriag2(Points pts,Triangles triag) {
		int indice = 0;
		float a1 = 
			((float)triag.sommet1.yValue-(float)triag.sommet2.yValue)/
			((float)triag.sommet1.xValue-(float)triag.sommet2.xValue);
		float b1 = (float)triag.sommet1.yValue - a1*(float)triag.sommet1.xValue;
		if((pts.yValue-a1*(float)pts.xValue+b1) > 0) {
			indice++;
		}
		float a2 = 
			((float)triag.sommet3.yValue-(float)triag.sommet2.yValue)/
			((float)triag.sommet3.xValue-(float)triag.sommet2.xValue);
		float b2 = (float)triag.sommet3.yValue - a2*(float)triag.sommet3.xValue;
		if((pts.yValue-a2*(float)pts.xValue+b2) > 0) {
			indice++;
		}
		float a3 = 
			((float)triag.sommet1.yValue-(float)triag.sommet3.yValue)/
			((float)triag.sommet1.xValue-(float)triag.sommet3.xValue);
		float b3 = (float)triag.sommet1.yValue - a3*(float)triag.sommet1.xValue;
		if((pts.yValue-a3*(float)pts.xValue+b3) > 0) {
			indice++;
		}
		return (indice==2)? true : false;
	}
	
	public static void createNewTriag(Points pts,Triangles triag,int indiceDel) {
		triangles.remove(indiceDel);
		triangles.add(new Triangles(triag.sommet1,triag.sommet2,pts));
		triangles.add(new Triangles(triag.sommet1,triag.sommet3,pts));
		triangles.add(new Triangles(triag.sommet2,triag.sommet3,pts));
	}
	
	public static void transDelaunay() {
		try {
		next = true;
		while(next) {
			Fenetre.imageTransformee.repaint();
			Thread.sleep(10);
		}
		boolean continuer = true;
		int iteration = 0;
		int i = 0;
		int j = 0;
		while(continuer) {
			iteration++;
			continuer = false;
			for(i=0;i<triangles.size();i++) {
				Triangles triag = (Triangles)triangles.get(i);
				for(j=0;j<triangles.size();j++) {
					Triangles triagComp = (Triangles)triangles.get(j);
					if(triag.isComplete(triagComp)) {
						if(triag.becomeDelaunay(triagComp)) {
							continuer = true;
							triangles.remove(i);
							if(i<j) {
								triangles.remove(j-1);
							}
							else {
								triangles.remove(j);
							}
							Fenetre.imageTransformee.repaint();
							/*
							next = true;
							while(next) {
								Fenetre.imageTransformee.repaint();
								Thread.sleep(10);
							}
							*/
							Fenetre.logsPrincipal.setText(
									"Triangles :"+triangles.size()+
									" - Calcul : "+
									(i*triangles.size()+j+1)+"/"+
									(triangles.size()*triangles.size()));
							triag = (Triangles)triangles.get(i);
							i = 0;
							j = triangles.size();
						}
					}
				}
			}
		}
		Fenetre.logsPrincipal.setText(
				"Termine "+(i*triangles.size()+j+1)+" / "+(triangles.size()*triangles.size()));
		}
		catch(Exception e) {
			System.out.println(e);
			e.printStackTrace();
		}
	}
	
	public static float calcAire(Triangles triag) {
		float aire = Math.abs(0.5f *(
			((float)triag.sommet2.xValue-(float)triag.sommet1.xValue)*
			((float)triag.sommet3.yValue-(float)triag.sommet1.yValue) -
			((float)triag.sommet3.xValue-(float)triag.sommet1.xValue)*
			((float)triag.sommet2.yValue-(float)triag.sommet1.yValue)
			));
		return aire;
	}
	
	public static void eraseBigTriag() {
		for(int i=0;i<triangles.size();i++) {
			Triangles tri = (Triangles)triangles.get(i);
			if(
					tri.sommet1.equals(bigTriag.sommet1) ||
					tri.sommet1.equals(bigTriag.sommet2) ||
					tri.sommet1.equals(bigTriag.sommet3) ||
					tri.sommet2.equals(bigTriag.sommet1) ||
					tri.sommet2.equals(bigTriag.sommet2) ||
					tri.sommet2.equals(bigTriag.sommet3) ||
					tri.sommet3.equals(bigTriag.sommet1) ||
					tri.sommet3.equals(bigTriag.sommet2) ||
					tri.sommet3.equals(bigTriag.sommet3)
					) {
				triangles.remove(i);
				Fenetre.imageTransformee.repaint();
				i--;
			}
		}
	}
	
}
